package geowiki;

import geowiki.data.LinkExtractor;
import geowiki.data.Place;
import geowiki.data.PlaceExtractor;
import geowiki.data.PlacemakerPlaceExtractor;
import geowiki.data.YQLLinkExtractor;
import geowiki.data.YQLPlaceExtractor;
import geowiki.utils.WikipediaUtils;
import geowiki.vis.PlaceMarker;

import java.util.ArrayList;
import java.util.List;

import processing.core.PApplet;
import processing.core.PImage;

/**
 * Basic application providing simple display of place markers and methods to extract places from articles.
 */
public class GeoWikiApp extends PApplet {

	/** Holds all visual place representations. */
	List<PlaceMarker> placeMarkers = new ArrayList<PlaceMarker>();
	List<PlaceMarker> linkedPlaceMarkers = new ArrayList<PlaceMarker>();

	PImage worldMap;

	public void setup() {
		size(1200, 600);
		smooth();

		worldMap = loadImage("worldmap-equirectangular-blank.png");
	}

	public void draw() {
		image(worldMap, 0, 0, width, height);

		for (int i = 0; i < placeMarkers.size(); i++) {
			PlaceMarker pm = (PlaceMarker) placeMarkers.get(i);
			pm.display();
		}
	}

	/**
	 * Creates place markers from the given places.
	 */
	List<PlaceMarker> createPlaceMarkers(List<Place> places) {
		List<PlaceMarker> placeMarkers = new ArrayList<PlaceMarker>();
		for (int i = 0; i < places.size(); i++) {
			Place place = places.get(i);
			PlaceMarker placeMarker = new PlaceMarker(this, place);
			placeMarkers.add(placeMarker);
		}
		return placeMarkers;
	}

	/**
	 * Extract places from a Wikipedia article.
	 * 
	 * @param wikipediaArticleName
	 *            The name of the Wikipedia article.
	 * @return All found places.
	 */
	public List<Place> loadPlacesViaYQL(String wikipediaArticleName) {
		String wikipediaURL = WikipediaUtils.getWikipediaArticleURL(wikipediaArticleName);
		PlaceExtractor placeExtractor = new YQLPlaceExtractor(this);
		List<Place> places = placeExtractor.getPlaces(wikipediaURL);
		return places;
	}

	public List<Place> loadPlacesViaPlacemakerFromWiki(String wikipediaArticleName) {
		String wikipediaURL = WikipediaUtils.getWikipediaArticleURL(wikipediaArticleName);
		LinkExtractor linkExtractor = new YQLLinkExtractor(this);
		String content = linkExtractor.getContent(wikipediaURL);

		PlacemakerPlaceExtractor placeExtractor = new PlacemakerPlaceExtractor(this);
		List<Place> places = placeExtractor.getPlaces(content);
		return places;
	}
}
